<?php
if(!defined('ACCESS')) {exit('Access denied.');}
class Card extends Base {
	// 表名
	private static $table_name = 'card';
	// 查询字段
	private static $columns = 'id,team_name,team_type';
	
	public static function getTableName(){
		return "card";
	}

	public static function getList($card_no,$open_phone,$start_time,$end_time) {
		$db=self::__instance();
		$conditon=array();

		$where = " where 1=1 ";
		if ($card_no) {
			$where = $where." and c.card_no='$card_no' ";
		}
		if ($open_phone) {
			$where = $where." and c.open_phone='$open_phone' ";
		}
		if ($start_time) {
			$start_time = "'".$start_time." 00:00:00'";
			$where = $where." and c.open_time=$start_time ";
		}

		if ($end_time) {
			$end_time = "'".$end_time." 00:00:00'";
			$where = $where." and c.open_time<=$end_time ";
		}

		$sql= "SELECT 
				    c.id,c.card_no,c.operator,c.card_type,c.dead_time,c.open_time,c.open_phone,p.phone,c.channel
				FROM
				    card c left join tb_phone_card tc on tc.card_id = c.id
				    left join tb_phone p on p.id=tc.phone_id $where order by c.card_no desc";
		$list = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);// self::getTableName(), self::$columns, $condition );
	
		if ($list) {
			return $list;
		}
		return  array();
	}

	public static function getCardBySim($sim=null) {
		$db=self::__instance();
		
		$sql= "select * from card where card_no='$sim'";
		$list = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);// self::getTableName(), self::$columns, $condition );

		if ($list) {
			return $list[0];
		}
		return false;
	}

	public static function getById($sim=null) {
		$db=self::__instance();
		
		$sql= "select * from card where id=$sim";
		$list = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);// self::getTableName(), self::$columns, $condition );

		if ($list) {
			return $list[0];
		}
		return array ();
	}

	public static function addCard($array) {
		$db=self::__instance();
		$id = $db->insert ( "card", $array );
		
		return $id;
	}

	public static function updateCard($array,$id) {
		$condition['AND']=array("id"=>$id);

		$db=self::__instance();
		$id = $db->update ("card", $array,$condition);
		return $id;
	}

	public static function findPhone($open_time=null,$card_type) {
		$db=self::__instance();

		$time = strtotime($open_time);
		$time_arr = "";
		$count =intval($card_type);
		$end_time = date("Y-m-d",$time)." 00:00:00";

		$where = " 1=1 ";
		for ($i=0; $i < $count; $i++) { 
			$time_where = "'".date("Y-m-d",$time+60*60*24*$i)." 00:00:00'";
			$where = $where ." and (c.open_time> $time_where or c.end_time<$time_where) ";
		}
		$time_arr = substr($time_arr, 0,strlen($time_arr)-1);
		$sql= "select * from (
				SELECT 
				    count(p.id) as count,p.id
				FROM
				    tb_phone p
				        LEFT JOIN
				    tb_phone_card c ON c.phone_id = p.id
				WHERE
				    ($where)
				        OR (c.open_time IS NULL)
				GROUP BY p.id order by p.phone asc) t order by t.count desc";
		$list = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);// self::getTableName(), self::$columns, $condition );

		if ($list) {
			return $list[0]["id"];
		}
		return null;
	}

	public static function getPhoneList($open_time=null) {
		$db=self::__instance();

		$time = strtotime($open_time);
		$time_arr = "";
		$count =intval($card_type);
		$end_time = date("Y-m-d",$time)." 00:00:00";

		$where = " c.end_time<'$open_time"." 00:00:00'";
		$time_arr = substr($time_arr, 0,strlen($time_arr)-1);
		$sql= "select * from (
				SELECT 
				    count(p.id) as count,p.id,p.phone
				FROM
				    tb_phone p
				        LEFT JOIN
				    tb_phone_card c ON c.phone_id = p.id
				WHERE
				    ($where)
				        OR (c.open_time IS NULL)
				GROUP BY p.id order by p.phone asc) t order by t.count desc";
		$list = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);// self::getTableName(), self::$columns, $condition );

		if ($list) {
			return $list;
		}
		return null;
	}

	public static function open($card_id,$sim,$type,$phone,$open_time,$phone_id) {
		$condition['AND']=array("card_no"=>$sim);

		$db=self::__instance();

		$id = $db->update ("card", array("open_phone"=>$phone, 'open_time'=>$open_time." 00:00:00") ,$condition);
		$time = strtotime($open_time);
		$count =intval($type);

		$end_time = date("Y-m-d",$time+60*60*24*$count)." 00:00:00";
		$id = $db->insert ("tb_phone_card", array('card_id' => $card_id,'end_time'=>$end_time,'open_time'=>$open_time." 00:00:00",'end_time'=>$end_time));
		return $id;
	}

	public static function assignPhone($card_id,$phone_id) {
		$condition['AND']=array("card_id"=>$card_id);

		$db=self::__instance();
		$id = $db->update ("tb_phone_card", array('phone_id'=>$phone_id) ,$condition);
		return $id;
	}

	public static function reopen($card_id,$type,$open_time,$phone_id) {
		$condition['AND']=array("id"=>$card_id);

		$db=self::__instance();

		$id = $db->update ("card", array('open_time'=>$open_time." 00:00:00") ,$condition);
		$time = strtotime($open_time);
		$count =intval($type);

		$end_time = date("Y-m-d",$time+60*60*24*$count)." 00:00:00";
		$id = $db->insert ("tb_phone_card", array('card_id' => $card_id,'end_time'=>$end_time,'open_time'=>$open_time." 00:00:00",'end_time'=>$end_time));
		return $id;
	} 

	public static function deleteCardOpen($card_id,$open_time) {
		$condition['AND']=array("card_id"=>$card_id,"open_time"=>$open_time);
		$db=self::__instance();
		$id = $db->delete ( "tb_phone_card" ,$condition);
	}

	public static function lookNews($team_id) {
		$condition['AND']=array("id"=>$team_id);

		$db=self::__instance();
		$id = $db->exec ( "UPDATE tb_news SET looks = looks+1 WHERE id = $team_id");
		return $id;
	}

	public static function delete($vs_id) {

		$condition['AND']=array("id"=>$vs_id);
		$db=self::__instance();
		$id = $db->delete ( self::getTableName() ,$condition);
		$condition['AND']=array("card_id"=>$vs_id);
		$id = $db->delete ( "tb_phone_card" ,$condition);

		return $id;
	}
}
